package net.lwf.factory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.hibernate.dao.worksheet.WorkSheetDAO;
import net.lwf.hibernate.entity.worksheet.WorkSheetInfo;


public class WorkSheetFactory {
	
	private static WorkSheetFactory instance = null;
	private Map<String, List<WorkSheetInfo>> wsinfo;
	private Map<String, Integer> wsbygrade = new HashMap<String, Integer>();

	private WorkSheetFactory() {
	}

	public synchronized final static WorkSheetFactory getInstance(){
		if (instance == null) {
			instance = new WorkSheetFactory();
		}
		return instance;
	}
	
	public List<WorkSheetInfo> getWorkSheetInfo(String grade, String subject) {
		return getWorkSheetInfo(grade, subject, null);
	}
	
	public int getWorkSheetCount(String grade) {
		Integer value = null;
		if(wsbygrade.containsKey(grade)) {
			value = wsbygrade.get(grade);
		}
		else {
			WorkSheetDAO dao = new WorkSheetDAO();
			value = new Integer(dao.getAvailableWorkSheets(grade));
			wsbygrade.put(grade, new Integer(dao.getAvailableWorkSheets(grade)));
		}
		return value.intValue();
	}
	
	public List<WorkSheetInfo> getWorkSheetInfo(String grade, String subject, String topic) {
		List<WorkSheetInfo> info;
		String key = grade + subject + topic;
		if(topic == null) {
			key = grade + subject;
		}
		if(wsinfo == null) {
			wsinfo = new HashMap<String, List<WorkSheetInfo>>();
		}
		
		if(wsinfo.containsKey(key)) {
			//info = (List<WorkSheetInfo>) wsinfo.get(key);
			info = wsinfo.get(key);
		}
		else {
			WorkSheetDAO dao = new WorkSheetDAO();
			info = dao.getWorkSheetInfo(grade, subject, topic);
			wsinfo.put(key, info);
		}
		return info;
	}

}
